package tech.aistar.corejava.day12.factory;

import tech.aistar.corejava.day12.Book;

import java.util.Comparator;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/7/30
 * @Version: 1.0
 */
public class ComparatorFactory {
    public static final int NAME = 0;
    public static final int PRICE = 1;

    //根据传入的参数,来返回某个具体的实例
    //面向接口编程 - 方法的返回类型写成接口,方法的返回值可以是这个接口的实现类
    public static Comparator<Book> getInstance(int type){
        Comparator<Book> c = null;

        switch (type){
            case NAME:
                //匿名内部类
                c = new Comparator<Book>() {
                    @Override
                    public int compare(Book o1, Book o2) {
                        return o1.getBname().compareTo(o2.getBname());
                    }
                };
                break;
            case PRICE:
                c = new Comparator<Book>() {
                    @Override
                    public int compare(Book o1, Book o2) {
                        if(o1.getPrice()>o2.getPrice()){
                            return 1;
                        }else if(o1.getPrice()<o2.getPrice()) {
                            return -1;
                        }else{
                            return 0;
                        }
                    }
                };
                break;
        }
        return c;
    }


}
